/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Chitiethoadon;
import pojo.Chitietphieunhaphang;
import pojo.Hoadonbanhang;
import pojo.Mathang;
import util.HibernateUtil;

/**
 *
 * @author FT
 */
public class ChiTietHoaDonDAO {
    public static List<Chitiethoadon> layDanhSachChiTietHoaDon() {
        List<Chitiethoadon> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sv from Chitiethoadon sv";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    
    public static String MaTangTuDong() {
        String chuoi = "";
        String macuoicung = "";
        List<Chitiethoadon> listpnh = ChiTietHoaDonDAO.layDanhSachChiTietHoaDon();
        if (ChiTietHoaDonDAO.layDanhSachChiTietHoaDon().size()==0){
            chuoi += "chd001";
            return chuoi;
        }
        macuoicung += listpnh.get(ChiTietHoaDonDAO.layDanhSachChiTietHoaDon().size() - 1).getIdchitiethoadon();
        String index = macuoicung.substring(3);
        try {

            //System.out.println("Index la"+index+"");
            int chiso = Integer.valueOf(index);
            chiso++;

            if (chiso < 10) {
                chuoi += "chd00" + chiso;
            } else if (chiso >= 10 && chiso < 100) {
                chuoi += "chd0" + chiso;
            } else if (chiso >= 1000) {
                chuoi += "chd" + chiso;
            }
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }
    
    public static void Insert(String machitiethoadon, String mahoadon, String mathang, int soluong, int thanhtien){
         Session session = HibernateUtil.getSessionFactory().openSession();
         
         Chitiethoadon cthd = new Chitiethoadon(machitiethoadon);
         Hoadonbanhang hd = HoaDonBanHangDAO.GetHoaDon(mahoadon);
         cthd.setHoadonbanhang(hd);
         Mathang mh = MatHangDAO.GetMatHang(mathang);
         cthd.setMathang(mh);
         cthd.setSoluong(soluong);
         cthd.setThanhtien(thanhtien);
         try {
            session.beginTransaction();
           
            session.save(cthd);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
    }
    public static Chitiethoadon GetChiTietHoaDon(String machitiet){
        Chitiethoadon cthd = new Chitiethoadon();
        List<Chitiethoadon> listcthd = ChiTietHoaDonDAO.layDanhSachChiTietHoaDon();
        for (int i=0;i<listcthd.size();i++){
            if (machitiet.equals(listcthd.get(i).getIdchitiethoadon())){
                cthd = listcthd.get(i);
            }
        }
        return cthd;
    }
    
    
    public static void DeleteOne(String machitiethoadon){
         Session session = HibernateUtil.getSessionFactory().openSession();
         Chitiethoadon cthd = ChiTietHoaDonDAO.GetChiTietHoaDon(machitiethoadon);
          try {
            session.beginTransaction();
            session.delete(cthd);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
    }
    
    public static void DeleteHoaDon(String mahoadon){
        List<Chitiethoadon> listcthd = ChiTietHoaDonDAO.layDanhSachChiTietHoaDon();
        for (int i=0;i<listcthd.size();i++){
            if (mahoadon.equals(listcthd.get(i).getHoadonbanhang().getIdhoadonbanhang())){
                ChiTietHoaDonDAO.DeleteOne(listcthd.get(i).getIdchitiethoadon());
            }
        }
    }
}
